package com.superman.supermarket.client.composite.home.storelist;

import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import com.superman.mvpframe.client.utils.MobileUtils;
import com.superman.mvpframe.client.utils.XJsonUtil;
import com.superman.mvpframe.client.widget.lazy.LazyImage;
import com.superman.supermarket.client.composite.IUIModule;
import com.superman.supermarket.client.composite.home.storelist.goods.GoodsModule;
import com.superman.supermarket.client.utils.ActivityJumpUrl;

/**
 * 推荐的店铺列表模块
 * 
 * @author superman
 * @version 2018年7月21日下午9:02:38
 */
public class StoreListModule extends Composite implements IUIModule {

	interface StoreListModuleUiBinder extends UiBinder<Widget, StoreListModule> {
	}

	private static StoreListModuleUiBinder uiBinder = GWT.create(StoreListModuleUiBinder.class);

	@UiField
	LazyImage bigimg;
	@UiField
	FlowPanel goodslist;

	private String storeId;

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年7月21日 下午9:02:40
	 */
	public StoreListModule() {
		initWidget(uiBinder.createAndBindUi(this));
		goodslist.getElement().getStyle().setProperty("marginTop", "-" + MobileUtils.px2(40) + "px");
	}

	@Override
	public Map<String, Object> getData() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setData(Map<String, Object> map) {
		this.storeId = String.valueOf(map.get("STORE_ID"));
		bigimg.setUrl(String.valueOf(map.get("IMAGE_ADDR")));

		if (map.get("SKUS") != null) {
			List<Map<String, Object>> skuList = XJsonUtil.getList((JSONArray) map.get("SKUS"));
			for (int i = 0; i < skuList.size(); i++) {
				GoodsModule goodsModule = new GoodsModule();
				goodsModule.setData(skuList.get(i));
				goodslist.add(goodsModule);

				if (i == 0)
					goodsModule.getElement().getStyle().setProperty("marginLeft", MobileUtils.px2(40) + "px");
				else
					goodsModule.getElement().getStyle().setProperty("marginLeft", MobileUtils.px2(10) + "px");
			}

			HTML _blank = new HTML();
			_blank.setWidth(MobileUtils.px2(40) + "px");
			_blank.setHeight("4.24rem");
			goodslist.add(_blank);
		} else {
			goodslist.removeFromParent();
		}

	}

	@UiHandler("bigimg")
	void toStorePage(ClickEvent event) {
		ActivityJumpUrl.pushToStorePage(storeId);
	}

}
